<?php
 if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class User extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        
        $this->data['controller'] = 'user';         
        $this->load->model('Muser');
    }
    
    
    public function logout()
    {
        $this->session->sess_destroy();
        redirect('user/login');
    }
    
    
    public function login()
    {
        
        $this->load->library('form_validation');
        $this->form_validation->set_rules('username', 'Tài khoản', 'required|xss_clean');
        $this->form_validation->set_rules('password', 'Mật khẩu', 'required|xss_clean');
        
        if ($this->form_validation->run() === TRUE)
        {
            $result = $this->Muser->loginAdmin();
            if($result == 200)
            {
                redirect('place');
            }else if($result == 201){
                $this->data['result'] = 'Sai mật khẩu';
            }else if($result == 202){
                $this->data['result'] = 'Tài khoản không tồn tại';
            }
        }
        
        $this->load->view('temp/user/login', $this->data);
    }
    
    public function update()
    {
        $this->checkAdmin($this->session->userdata('id_user'));
        $this->data['data'] = $this->Muser->getUserInfo($this->session->userdata('id_user'));
        $this->load->library('form_validation');
        $this->form_validation->set_rules('fullname', 'Họ tên', 'required|xss_clean');
        $this->form_validation->set_rules('email', 'Email', 'required|xss_clean');
        $this->form_validation->set_rules('mobile', 'Số điện thoại', 'required|xss_clean|min_length[10]|max_length[12]');
        
        if ($this->form_validation->run() === TRUE)
        {
            /*$dataimage = $this->getParamString('img_hidden');
            if(!in_array($dataimage, array(null, '', '0')))
            {
                $timenow = time().rand(0,9999);
                $nameimagess = 'nhatnv-'.$timenow.'.png';
                $paths = PROJECT_PATH.'/public/upload/';
                $exp = explode("[removed]",$dataimage);
                $this->savefile($exp[1],$paths,$nameimagess);
                $img = 'public/upload/'.$nameimagess;
            }else{
                $img = '';
            }*/
            
            $avatar_check = $this->getParamString('image_check');
            $avatar_verify = $this->getParamString('image_verify');
            
            $avatar = $this->getParamString('image_');//die($bground);
            if(!in_array($avatar_verify, array(null, '')) && ($avatar_verify != $avatar_check))
            {
                @unlink(PROJECT_PATH.'/'.$avatar_verify);
            }
            
            if(!in_array($avatar, array(null, '', '0')))
            {
                $timenow = time().rand(0,9999);
                $nameimagess = 'nhatnv-'.$timenow.'.png';
                $paths = PROJECT_PATH.'/public/upload/';
                $exp = explode("[removed]",$avatar);
                $this->savefile($exp[1],$paths,$nameimagess);
                $img = 'public/upload/'.$nameimagess;
            }else{
                $img = '';
            }
            
            $this->data['result'] = $this->Muser->updateInfo($this->session->userdata('id_user'), $img);
            die('<meta charset="utf-8"><script>alert("Cập nhật thành công");window.location.href="'.$this->data['baseurl'].'setting.html";</script>');
        }
        $this->data['action'] = 'update';
        $this->load->view('temp/layout', $this->data);
    }
    
    public function updatepassword()
    {
        $this->checkAdmin($this->session->userdata('id_user'));
        $this->load->library('form_validation');
        $this->form_validation->set_rules('currentPassword', 'Mật khẩu hiện tại', 'required|xss_clean');
        $this->form_validation->set_rules('password', 'Mật khẩu mới', 'required|xss_clean|matches[passwordVerify]');
        
        if ($this->form_validation->run() === TRUE)
        {
			
            $this->data['result'] = $this->Muser->updatepasswordDB($this->session->userdata('id_user'));
            if($this->data['result'] == 200)
            {
                die('<meta charset="utf-8"><script>alert("Cập nhật thành công");window.location.href="'.$this->data['baseurl'].'setting.html";</script>');
            }else{
                $this->data['result'] = $this->data['result'];
            }
        }
        $this->data['action'] = 'passwd';
        $this->load->view('temp/layout', $this->data);
    }
    
    public function account()
    {
        $this->checkPermission($this->session->userdata('level'), array(1));
        $this->data['id_active'] = $this->getParamUri(2,3);
        $this->load->library('pagination');
        $config['per_page'] = 10;
        $this->data['page'] = $this->getParamUri(2,3);
        $data = $this->Muser->getUsers($config['per_page'],(($this->getParamUri(2,3) - 1) * $config['per_page'] > 0 ? ($this->getParamUri(2,3) - 1) * $config['per_page'] : 0));
        $config['base_url'] = base_url($this->uri->segment(1).'/');//$this->uri->segment(2).'/');
        $config['total_rows'] = $data['count'];
        $config['uri_segment'] = 2;
        $config['uri_segment_page'] = $this->getParamUri(2,3);
        $config['suffix'] = '.html';
        $this->pagination->initialize($config);
        $this->data['pageg'] = $this->data['page'] - 1 > 0 ? $this->data['page'] - 1 : 0;
        $this->data['data'] = $data['data'];
        $this->data['action'] = 'account';
        $this->load->view('temp/layout', $this->data);
    }
    
    
    public function addaccount()
    {
        $this->checkPermission($this->session->userdata('level'), array(1));
        $this->checkAdmin($this->session->userdata('id_user'));
        $this->load->library('form_validation');
        $this->form_validation->set_rules('username', 'Tài khoản', 'required|xss_clean|is_unique[user.username]');
        $this->form_validation->set_rules('password', 'Mật khẩu', 'required|xss_clean|matches[passwordVerify]');
        $this->form_validation->set_rules('fullname', 'Họ và tên', 'required|xss_clean');
        $this->form_validation->set_rules('email', 'Email', 'required|xss_clean|valid_email');
        $this->form_validation->set_rules('mobile', 'Số điện thoại', 'required|xss_clean');
        $this->form_validation->set_rules('level', 'Loại tài khoản', 'required');
        
        if ($this->form_validation->run() === TRUE)
        {
            $avatar = $this->getParamString('image_');//die($bground);            
            if(!in_array($avatar, array(null, '', '0')))
            {
                $timenow = time().rand(0,9999);
                $nameimagess = 'nhatnv-'.$timenow.'.png';
                $paths = PROJECT_PATH.'/public/upload/';
                $exp = explode("[removed]",$avatar);
                $this->savefile($exp[1],$paths,$nameimagess);
                $img = 'public/upload/'.$nameimagess;
            }else{
                $img = '';
            }
            
            $this->data['result'] = $this->Muser->addAccount($this->session->userdata('id_user'), $img);
            if($this->data['result'] == 200)
            {
                die('<meta charset="utf-8"><script>alert("Thêm thành công");window.location.href="'.$this->data['baseurl'].'account.html";</script>');
            }else{
                $this->data['result'] = $this->data['result'];
            }
        }
        $this->data['action'] = 'addaccount';
        $this->load->view('temp/layout', $this->data);
    }
    
    public function editaccount()
    {
        $this->checkPermission($this->session->userdata('level'), array(1));
        $this->checkAdmin($this->session->userdata('id_user'));
        $this->load->library('form_validation');
        $this->form_validation->set_rules('fullname', 'Họ và tên', 'required|xss_clean');
        $this->form_validation->set_rules('email', 'Email', 'required|xss_clean|valid_email');
        $this->form_validation->set_rules('mobile', 'Số điện thoại', 'required|xss_clean');
        $this->form_validation->set_rules('level', 'Loại tài khoản', 'required');
        $this->data['id'] = $this->getParamInt('id');
        $this->data['data'] = $this->Muser->getListDataUser($this->data['id']); 
        if ($this->form_validation->run() === TRUE)
        {
            /*$dataimage = $this->getParamString('img_hidden');
            if(!in_array($dataimage, array(null, '', '0')))
            {
                $timenow = time().rand(0,9999);
                $nameimagess = 'nhatnv-'.$timenow.'.png';
                $paths = PROJECT_PATH.'/public/upload/';
                $exp = explode("[removed]",$dataimage);
                $this->savefile($exp[1],$paths,$nameimagess);
                $img = 'public/upload/'.$nameimagess;
            }else{
                $img = '';
            }*/
            
            $avatar_check = $this->getParamString('image_check');
            $avatar_verify = $this->getParamString('image_verify');
            
            $avatar = $this->getParamString('image_');//die($bground);
            if(!in_array($avatar_verify, array(null, '')) && ($avatar_verify != $avatar_check))
            {
                @unlink(PROJECT_PATH.'/'.$avatar_verify);
            }
            
            if(!in_array($avatar, array(null, '', '0')))
            {
                $timenow = time().rand(0,9999);
                $nameimagess = 'nhatnv-'.$timenow.'.png';
                $paths = PROJECT_PATH.'/public/upload/';
                $exp = explode("[removed]",$avatar);
                $this->savefile($exp[1],$paths,$nameimagess);
                $img = 'public/upload/'.$nameimagess;
            }else{
                $img = '';
            }
            
            $this->data['result'] = $this->Muser->editAccountDB($this->session->userdata('id_user'), $img, $this->data['id']);
            if($this->data['result'] == 200)
            {
                die('<meta charset="utf-8"><script>alert("Cập nhật thành công");window.location.href="'.$this->data['baseurl'].'account.html";</script>');
            }else{
                $this->data['result'] = $this->data['result'];
            }
        }
        $this->data['action'] = 'editaccount';
        $this->load->view('temp/layout', $this->data);
    }
    
    public function deleteaccount()
    {
        $this->checkPermission($this->session->userdata('level'), array(1));
        $id = $this->getParamInt('id');
        $run = $this->Muser->deleteAccountDb($id);
        if($run == 200)
        {
            die('<meta charset="utf-8"><script>alert("Xóa thành công");window.location.href="'.$this->data['baseurl'].'account.html";</script>');
        }
    }
    
    public function getagencyajax()
    {
        $this->checkPermission($this->session->userdata('level'), array(1));
        $agency = $this->getParamInt('id');
        if($agency != 0)
        {
            $this->data['selected'] = $agency;
        }
        $this->data['data'] = $this->Muser->getAgencyAjaxDb();
        $this->data['action'] = 'getagencyajax';
        $this->load->view('temp/user/getagencyajax', $this->data);
    }
	
	public function getplaceajax()
    {
        $this->checkPermission($this->session->userdata('level'), array(1));
		$place = $this->getParamInt('id');
        if($place != 0)
        {
            $this->data['selected'] = $place;
        }
        $this->data['data'] = $this->Muser->getPlaceAjaxDb();
        $this->data['action'] = 'getplaceajax';
        $this->load->view('temp/user/getplaceajax', $this->data);
    }
	
    
}
